<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%
	request.setAttribute("path", request.getContextPath());
%>
<script type="text/javascript">
function validateUserInfo(){
	$("#id").change(function (){
		if(check.userId("id")){
			$.post("${root}/app/user/exist/"+$("#id").val(),function(result){
				 if(result){
				 	common.showClientErrorTip("id",'<spring:message code="err0004"/>');
				 }else{
					 common.destoryTip("id");
				}
			});
		}
	});

	$("#password").change(function (){
		check.password("password");
	});
	
	$("#name").change(function (){
		check.emptyCheck("name");
	});
	
	$("#mobile").change(function (){
		check.mobile("mobile");
	});
	$("#phone").change(function (){
		check.phone("phone");
	});
	
	$("#age").change(function(){
		check.intNum("age");
	});
	
	$("#email").change(function(){
		check.email("email");
	});
	
}
function saveUser(){
	
	var isModify = $("#modifyFlag").val();
	var uri = "${root}/app/user/add"
	var formData = $("#userForm").serialize() + "&" + $("#groupForm").serialize()
			
	if(isModify == "true"){
		uri = "${root}/app/user/modify"
	   	if( check.emptyCheck("name") & check.password("password") & check.mobile("mobile") ){
			if(validateExt()){
				common.ajaxSubmitWithData(uri,formData,"保存用户失败","",'common.reload("${root}/view/page?loc=user_list&h=h01&n=n01")');
			}
		}
		
	}
	
	if(isModify == "false"){
		
		if(check.userId("id") & check.emptyCheck("name") & (check.emptyCheck("password") && check.password("password")) & check.mobile("mobile")){
		if(validateExt()){
			common.ajaxSubmitWithData(uri,formData,"新增用户失败","",'common.reload("${root}/view/page?loc=user_list&h=h01&n=n01")');
		}
		}
	}
	
}
function validateExt(){
	
	var staff = $("#position").find("option:selected").attr("type");
	if(staff == "staff"){
		if($("#secondOrg").val() == ""){
			common.modalGlobalMessage("请设置员工大区和办事处!"); 
			return false;
		}
	}
	return  true;
}
$(function () {
	validateUserInfo();
});
</script>
<jsp:include page="../../base/master_js.jsp"></jsp:include>
<form class="form-horizontal" id="userForm" role="form">
	<jsp:include page="../../base/token.jsp" />
	<input type="hidden" name="imageId" id="imageId" value="${user.imageId}" />
	<jsp:include page="user_base.jsp"></jsp:include>
	<div class="control-group">
	    <label class="control-label">密码:</label>
	    <div class="controls">
	    	<input type="hidden" />
	      <input type="text" class="input-xlarge" name="password" id="password"  >
	    </div>
  	</div>
	<div class="control-group">
	    <label class="control-label">身份:</label>
	    <div class="controls">
	      <select id="position" name="position" class="input-xlarge" >
					<c:forEach items="${positions }" var="item">
						<c:choose>
							<c:when test="${user.positionId == item.key }">
								<option value="${item.key }" type="${item.value.type }"
									selected="selected"><c:out value="${item.value.name }" /></option>
							</c:when>
							<c:otherwise>
								<option value="${item.key }" type="${item.value.type }"><c:out
										value="${item.value.name }" /></option>
							</c:otherwise>
						</c:choose>
					</c:forEach>
			</select>
	    </div>
  	</div>
  	<div class="control-group">
  		 <label class="control-label">大区(旧):</label>
  		  <div class="controls">
  		  	 <span class="input-xlarge uneditable-input"><c:out value="${user.firstOrg.name}"></c:out></span></p>
  		  </div>
  	</div>
  	
	<div class="control-group">
	    <label class="control-label">大区:</label>
	    <div class="controls">
			<select id="firstOrg" name="firstOrg" class="input-xlarge"  onchange="firstOrgChange(this,'secondOrg',true)">
				<option value=""></option>
				<c:forEach var="item" items="${firstOrgs}">
					<option value="${item.id }"><c:out value="${item.name }"/></option>
				</c:forEach>
			</select>
	    </div>
  	</div>
  	
  	 <div class="control-group">
  		 <label class="control-label">办事处(旧):</label>
  		  <div class="controls">
  		  	 <span class="input-xlarge uneditable-input"><c:out value="${user.secondOrg.name}"></c:out></span></p>
  		  </div>
  	</div>
	<div class="control-group">
	    <label class="control-label">办事处:</label>
	    <div class="controls">
			<select id="secondOrg" name="secondOrg"  class="input-xlarge" >
				<option value=""></option>
			</select>
	    </div>
  	</div>
  	<div class="control-group">
		<label class="control-label">用户角色:</label>
	 	<div class="controls">
  			<jsp:include page="user_group.jsp"></jsp:include>
  		</div>
	</div>
</form>
<jsp:include page="user_img.jsp"></jsp:include>
<form class="form-horizontal">
<div class="form-actions text-center">
      <button type="button" class="btn btn-success" onclick="saveUser()">保存</button>
</div>
</form>
